﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AtletIS.Shared.Data;

namespace AtletIS.Shared.Forms.Admin
{
    public partial class frmAdminCountries : Form
    {
        private aDbDataSet.CountriesRow drActiveCountry;
        private bool bNew;

        // Constructor
        public frmAdminCountries()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.AtletIS64;
        }

        // Load
        private void frmAdminCountries_Load(object sender, EventArgs e)
        {
            tsbCancel.Enabled = false;
            tsbSave.Enabled = false;
            this.countriesTableAdapter.Fill(this.aDbDataSet.Countries);

        }

        // Selection changed
        private void countriesDataGridView_SelectionChanged(object sender, EventArgs e)
        {
            if (countriesDataGridView.CurrentRow == null)
                return;

            try
            {
                DataRowView drv = (DataRowView)countriesDataGridView.CurrentRow.DataBoundItem;
                txtID.Text = drv["ID"].ToString();
                txtName.Text = drv["Name"].ToString();

                pbFlag.Image = null;
                string path = System.IO.Directory.GetCurrentDirectory() + "\\Ext\\Flags\\" + drv["ID"].ToString() + ".png";
                if (System.IO.File.Exists(path))
                { 
                    Image img = Image.FromFile(path);
                    pbFlag.Image = img;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Krajiny", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // New
        private void tsbNew_Click(object sender, EventArgs e)
        {
            try
            {
                bNew = true;
                tsbNew.Enabled = false;
                tsbEdit.Enabled = false;
                tsbCancel.Enabled = true;
                tsbSave.Enabled = true;

                txtID.Text = string.Empty;
                txtName.Text = string.Empty;

                pbFlag.Image = null;

                this.drActiveCountry = aDbDataSet.Countries.NewCountriesRow();

                this.drActiveCountry["Sync"] = false;

                gbList.Enabled = false;
                gbDetails.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Krajiny - pridať", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Edit
        private void tsbEdit_Click(object sender, EventArgs e)
        {
            try
            {
                bNew = false;
                tsbNew.Enabled = false;
                tsbEdit.Enabled = false;
                tsbCancel.Enabled = true;
                tsbSave.Enabled = true;

                DataRowView drv = (DataRowView)countriesDataGridView.CurrentRow.DataBoundItem;
                this.drActiveCountry = (aDbDataSet.CountriesRow)drv.Row;

                gbList.Enabled = false;
                gbDetails.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Krajiny - editovať", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Cancel
        private void tsbCancel_Click(object sender, EventArgs e)
        {
            tsbNew.Enabled = true;
            tsbEdit.Enabled = true;
            tsbCancel.Enabled = false;
            tsbSave.Enabled = false;

            gbList.Enabled = true;
            gbDetails.Enabled = false;

            countriesDataGridView_SelectionChanged(null, null);
        }

        // Save
        private void tsbSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtID.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Zadajte ID krajiny!", "Krajiny - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                if (txtName.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Zadajte názov!", "Kluby - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                this.drActiveCountry["ID"] = txtID.Text;
                this.drActiveCountry["Name"] = txtName.Text;
                this.drActiveCountry["Modified"] = DateTime.Now;

                if (bNew)
                {
                    aDbDataSet.Countries.Rows.Add(this.drActiveCountry);
                    this.countriesTableAdapter.Update(aDbDataSet.Countries);
                }
                else
                {
                    this.countriesTableAdapter.Update(this.drActiveCountry);
                }

                this.countriesTableAdapter.Fill(this.aDbDataSet.Countries);

                tsbNew.Enabled = true;
                tsbEdit.Enabled = true;
                tsbCancel.Enabled = false;
                tsbSave.Enabled = false;

                gbDetails.Enabled = false;
                gbList.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Krajiny - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Close
        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}
